Server பொருள்
Node.js இல் Server பொருள்கள் நெட்வொர்க் சேவையகங்களை உருவாக்க பயன்படுத்தப்படுகின்றன. வெவ்வேறு தொகுதிகள் அவற்றின் சொந்த Server செயலாக்கங்களை வழங்குகின்றன:
http.Server
HTTP சேவையகங்களை உருவாக்குவதற்கு
https.Server
HTTPS சேவையகங்களை உருவாக்குவதற்கு
net.Server
TCP சேவையகங்களை உருவாக்குவதற்கு
tls.Server
TLS/SSL சேவையகங்களை உருவாக்குவதற்கு
இந்த சேவையக பொருள்கள் client இணைப்புகளைக் கையாளுகின்றன, கோரிக்கைகளைச் செயலாக்குகின்றன மற்றும் அவற்றின் தொடர்புடைய நெறிமுறைகளுக்கு ஏற்றவாறு பதில்களை வழங்குகின்றன.
பொதுவான Server முறைகள்
| முறை | விளக்கம் |
|---|---|
| server.listen([port][, host][, backlog][, callback]) | இணைப்புகளுக்காக சேவையகம் கேட்கத் தொடங்குகிறது. சேவையகம் bind செய்யப்படும் போது callback செயல்படுத்தப்படும் |
| server.close([callback]) | புதிய இணைப்புகளை ஏற்க சேவையகத்தை நிறுத்துகிறது. அனைத்து இணைப்புகளும் மூடப்படும் போது callback அழைக்கப்படும் |
| server.address() | சேவையகத்தின் bind செய்யப்பட்ட முகவரி, முகவரி குடும்பப் பெயர் மற்றும் port ஐத் தரும் |
| server.getConnections(callback) | சேவையகத்தில் ஒரே நேரத்தில் உள்ள இணைப்புகளின் எண்ணிக்கையை அசிங்க்ரோனஸாகப் பெறுகிறது |
பொதுவான Server நிகழ்வுகள்
| நிகழ்வு | விளக்கம் |
|---|---|
| 'close' | சேவையகம் மூடப்படும் போது வெளியிடப்படுகிறது |
| 'connection' | ஒரு புதிய இணைப்பு உருவாக்கப்படும் போது வெளியிடப்படுகிறது |
| 'error' | பிழை ஏற்படும் போது வெளியிடப்படுகிறது |
| 'listening' | server.listen() அழைக்கப்பட்ட பிறகு சேவையகம் bind செய்யப்படும் போது வெளியிடப்படுகிறது |
HTTP சேவையகம்
Node.js இல் HTTP சேவையகம் http.createServer() முறையைப் பயன்படுத்தி உருவாக்கப்படுகிறது:
const http = require('http');
// ஒரு HTTP சேவையகத்தை உருவாக்கு
const server = http.createServer((req, res) => {
// கோரிக்கைகளைக் கையாள
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
});
// சேவையகத்தைத் தொடங்கு
const PORT = 8080;
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`);
});
// சேவையக நிகழ்வுகளைக் கையாள
server.on('error', (err) => {
console.error(`Server error: ${err.message}`);
});
server.on('close', () => {
console.log('Server closed');
});
HTTPS சேவையகம்
HTTPS சேவையகத்திற்கு SSL சான்றிதழ்கள் தேவைப்படுகின்றன மற்றும் https.createServer() முறையைப் பயன்படுத்தி உருவாக்கப்படுகிறது:
const https = require('https');
const fs = require('fs');
// SSL விருப்பங்கள் - ஒரு உற்பத்தி சூழலில், சரியாக கையொப்பமிடப்பட்ட சான்றிதழ்களைப் பயன்படுத்தவும்
const options = {
key: fs.readFileSync('server-key.pem'), // உங்கள் key கோப்பிற்கான பாதை
cert: fs.readFileSync('server-cert.pem') // உங்கள் certificate கோப்பிற்கான பாதை
};
// ஒரு HTTPS சேவையகத்தை உருவாக்கு
const server = https.createServer(options, (req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Secure World\n');
});
// சேவையகத்தைத் தொடங்கு
const PORT = 3443;
server.listen(PORT, () => {
console.log(`Server running at https://localhost:${PORT}/`);
});
TCP சேவையகம் (net.Server)
ஒரு TCP சேவையகம் net.createServer() முறையைப் பயன்படுத்தி உருவாக்கப்படுகிறது:
const net = require('net');
// ஒரு TCP சேவையகத்தை உருவாக்கு
const server = net.createServer((socket) => {
console.log('Client connected');
// Client இலிருந்து தரவைக் கையாள
socket.on('data', (data) => {
console.log(`Received: ${data}`);
socket.write(`Echo: ${data}`);
});
// Client துண்டிப்பைக் கையாள
socket.on('end', () => {
console.log('Client disconnected');
});
// Socket பிழைகளைக் கையாள
socket.on('error', (err) => {
console.error(`Socket error: ${err.message}`);
});
});
// சேவையகத்தைத் தொடங்கு
const PORT = 8888;
server.listen(PORT, () => {
console.log(`TCP server listening on port ${PORT}`);
});
// அது கேட்கும் பிறகு சேவையக தகவலைப் பெறு
server.on('listening', () => {
const address = server.address();
console.log(`Server info: ${JSON.stringify(address)}`);
});
TLS/SSL சேவையகம்
ஒரு பாதுகாப்பான TLS/SSL சேவையகம் tls.createServer() முறையைப் பயன்படுத்தி உருவாக்கப்படுகிறது:
const tls = require('tls');
const fs = require('fs');
// SSL விருப்பங்கள்
const options = {
key: fs.readFileSync('server-key.pem'),
cert: fs.readFileSync('server-cert.pem'),
// Client certificate கோருக (விருப்பமானது)
requestCert: true,
// சான்றிதழ்கள் இல்லாமல் இணைப்புகளை நிராகரிக்கவும் (விருப்பமானது)
rejectUnauthorized: false
};
// ஒரு TLS சேவையகத்தை உருவாக்கு
const server = tls.createServer(options, (socket) => {
console.log('Client connected securely');
// Client ஒரு certificate வழங்கியதா எனச் சரிபார்க்கவும்
if (socket.authorized) {
console.log('Client authorized');
} else {
console.log('Client unauthorized');
}
// Client இலிருந்து தரவைக் கையாள
socket.on('data', (data) => {
console.log(`Received: ${data}`);
socket.write(`Secure echo: ${data}`);
});
// Client துண்டிப்பைக் கையாள
socket.on('end', () => {
console.log('Client disconnected');
});
});
// சேவையகத்தைத் தொடங்கு
const PORT = 8443;
server.listen(PORT, () => {
console.log(`TLS server listening on port ${PORT}`);
});
Routing உடன் HTTP சேவையகம்
அடிப்படை routing உடன் ஒரு முழுமையான HTTP சேவையகம்:
const http = require('http');
const url = require('url');
// அடிப்படை routing உடன் ஒரு HTTP சேவையகத்தை உருவாக்கு
const server = http.createServer((req, res) => {
// URL ஐ parse செய்
const parsedUrl = url.parse(req.url, true);
const path = parsedUrl.pathname;
const trimmedPath = path.replace(/^\/+|\/+$/g, '');
// HTTP முறையைப் பெறு
const method = req.method.toLowerCase();
// வினா அளவுருக்களைப் பெறு
const queryParams = parsedUrl.query;
// கோரிக்கையை log செய்
console.log(`Request received: ${method} ${trimmedPath}`);
// Route handler
let response = {
status: 404,
contentType: 'application/json',
payload: { message: 'Not Found' }
};
// அடிப்படை routing
if (method === 'get') {
if (trimmedPath === '') {
// Home route
response = {
status: 200,
contentType: 'text/html',
payload: 'Home Page
Welcome to the server
'
};
} else if (trimmedPath === 'api/users') {
// API route - பயனர்களை பட்டியலிடு
response = {
status: 200,
contentType: 'application/json',
payload: {
users: [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' }
]
}
};
} else if (trimmedPath.startsWith('api/users/')) {
// API route - ID மூலம் பயனரைப் பெறு
const userId = trimmedPath.split('/')[2];
response = {
status: 200,
contentType: 'application/json',
payload: { id: userId, name: `User ${userId}` }
};
}
}
// பதிலைத் திரும்ப அனுப்பு
res.setHeader('Content-Type', response.contentType);
res.writeHead(response.status);
// payload ஒரு பொருளாக இருந்தால் string ஆக மாற்று
const payloadString = typeof response.payload === 'object'
? JSON.stringify(response.payload)
: response.payload;
res.end(payloadString);
});
// சேவையகத்தைத் தொடங்கு
const PORT = 8080;
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`);
});
சேவையக Timeouts மற்றும் வரம்புகள்
சேவையக timeouts மற்றும் இணைப்பு வரம்புகளை உள்ளமைத்தல்:
const http = require('http');
// ஒரு HTTP சேவையகத்தை உருவாக்கு
const server = http.createServer((req, res) => {
// ஒரு தாமதமான பதிலை உருவகப்படுத்துதல்
setTimeout(() => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Response after delay\n');
}, 2000);
});
// சேவையக timeouts ஐ உள்ளமை
server.timeout = 10000; // 10 விநாடிகள் (இயல்புநிலை 120000 அல்லது 2 நிமிடங்கள்)
server.keepAliveTimeout = 5000; // 5 விநாடிகள் (இயல்புநிலை 5000)
server.maxHeadersCount = 1000; // அதிகபட்ச தலைப்புகள் எண்ணிக்கை (இயல்புநிலை 2000)
server.maxRequestsPerSocket = 100; // ஒரு socket க்கு அதிகபட்ச கோரிக்கைகள் (Node.js 14+)
// சேவையகத்தைத் தொடங்கு
const PORT = 8080;
server.listen(PORT, () => {
console.log(`Server with timeouts configured at http://localhost:${PORT}/`);
// சேவையக உள்ளமைப்பைக் காட்டு
console.log(`Server timeout: ${server.timeout}ms`);
console.log(`Keep-alive timeout: ${server.keepAliveTimeout}ms`);
console.log(`Max headers count: ${server.maxHeadersCount}`);
console.log(`Max requests per socket: ${server.maxRequestsPerSocket || 'N/A'}`);
});
HTTP/2 சேவையகம்
ஒரு HTTP/2 சேவையகத்தை உருவாக்குதல் (Node.js v8.4.0 இல் அறிமுகப்படுத்தப்பட்டது):
const http2 = require('http2');
const fs = require('fs');
// HTTP/2 க்கான SSL விருப்பங்கள்
const options = {
key: fs.readFileSync('server-key.pem'),
cert: fs.readFileSync('server-cert.pem')
};
// ஒரு HTTP/2 சேவையகத்தை உருவாக்கு
const server = http2.createSecureServer(options);
// உள்வரும் streams களைக் கையாள
server.on('stream', (stream, headers) => {
const path = headers[':path'];
const method = headers[':method'];
console.log(`${method} ${path}`);
// கோரிக்கைக்குப் பதிலளி
stream.respond({
'content-type': 'text/html',
':status': 200
});
stream.end('HTTP/2 Server
This page was served via HTTP/2
');
});
// சேவையகத்தைத் தொடங்கு
const PORT = 8443;
server.listen(PORT, () => {
console.log(`HTTP/2 server running at https://localhost:${PORT}/`);
});